package org.example.module.mq;

import org.example.module.entity.SysUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
 
@RestController
public class SendMessageController {

    /**
     * 使用RabbitTemplate,这提供了接收/发送等等方法
     */
    @Autowired
    RabbitTemplate rabbitTemplate;
 
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        SysUser user = new SysUser();
        user.setUserId(1);
        user.setUserName("直连交换机");
        user.setCreateTime(System.currentTimeMillis() + "");
        user.setPassword(UUID.randomUUID().toString());
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", user);
        return "ok";
    }

    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1() {
        SysUser user = new SysUser();
        user.setUserId(1);
        user.setUserName("topic交换机");
        user.setCreateTime(System.currentTimeMillis() + "");
        user.setPassword(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", user);
        return "ok";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        SysUser user = new SysUser();
        user.setUserId(1);
        user.setUserName("topic交换机");
        user.setCreateTime(System.currentTimeMillis() + "");
        user.setPassword(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", user);
        return "ok";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        SysUser user = new SysUser();
        user.setUserId(1);
        user.setUserName("扇形交换机");
        user.setCreateTime(System.currentTimeMillis() + "");
        user.setPassword(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("fanoutExchange", null, user);
        return "ok";
    }
}